package Homework;

public class FizzBuzz {
    private int n;
    private int count = 1;

    public FizzBuzz(int n){
        this.n = n;
    }

    public void printFizz(){
        while(count<=n){
            synchronized (this) {
                if(count%3==0 && count%5!=0){
                    System.out.println("fizz");
                    count++;
                    notifyAll();
                }else{
                    try {
                        wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void printBuzz(){
        while(count<=n){
            synchronized (this) {
                if(count%5==0 && count%3!=0){
                    System.out.println("buzz");
                    count++;
                    notifyAll();
                }else{
                    try {
                        wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void printFizzbuzz(){
        while(count<=n){
            synchronized (this) {
                if(count%3==0 && count%5==0){
                    System.out.println("fizzbuzz");
                    count++;
                    notifyAll();
                }else{
                    try {
                        wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void printNumber(){
        while(count<=n){
            synchronized (this) {
                if(count%3!=0 && count%5!=0){
                    System.out.println(count);
                    count++;
                    notifyAll();
                }else{
                    try {
                        wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
